//
//  YLJ_StringTool.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/23.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "YLJ_StringTool.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation YLJ_StringTool
NSString* koString(NSString *str) {
    return defaultStrings(str,@"");
}

NSString* defaultStrings(NSString *str,NSString *def) {
    if ((NSNull *)str == [NSNull null]) {
        return @"";
    }
    if(str==nil)
    {
        if(def==nil)
        {
            return @"";
        }
        return def;
    }
    if ([str isKindOfClass:[NSString class]]) {
        if ([str isEqualToString:@"<null>"]) {
            return @"";
        }
    }
    
    return str;
}
NSString* numberString(NSString *str) {
    if ((NSNull *)str == [NSNull null]) {
        return @"0.00";
    }
    
    if ([str isKindOfClass:[NSString class]]) {
        if (judgeNULL(str)) {
            return @"0.00";
        }
        if([str isEqualToString:@"0"]||[str isEqualToString:@"0.00"]) {
            return @"0.00";
        }
        return str;
    }else{
        NSString *lsstring = [NSString stringWithFormat:@"%@",str];
        if([lsstring isEqualToString:@"0"]) {
            return @"0.00";
        }
        if([lsstring isEqualToString:@"(null)"]) {
            return @"0.00";
        }
        if (judgeNULL(lsstring)) {
            return @"0.00";
        }
        return lsstring;
    }
    
    
    
    return str;
}

BOOL judgeNULL(id obj) {
    if ([obj isKindOfClass:[NSString class]]) {
        NSString *temp = obj;
        if(temp == nil || temp.length == 0)
        {
            return YES;
        }
        return NO;
    }
    else if ([obj isKindOfClass:[NSArray class]]) {
        NSArray *temp = obj;
        if(temp == nil || temp.count == 0)
        {
            return YES;
        }
        return NO;
    }
    else if ([obj isKindOfClass:[NSDictionary class]]) {
        NSDictionary *temp = obj;
        if(temp == nil || temp.count == 0)
        {
            return YES;
        }
        return NO;
    }
    else if (obj == nil) {
        return YES;
    }
    else if ((NSNull *)obj == [NSNull null]) {
        return YES;
    }
    return NO;
}

id nullPPoo(id obj){
    BOOL null_Bool = judgeNULL(obj);
    if (null_Bool) {
        return @"";
    }else{
        return obj;
    }
}


+ (id)JSONStringParseWithString:(NSString *)JSONString {
    NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    id responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
    return responseJSON;
}
#pragma mark - 手机号中间四位换成＊＊＊＊
+ (NSString *)phoneUpdate:(NSString *)phone {
    //截取
    NSString *qian3=[phone substringWithRange:NSMakeRange(0,3)];
    NSString *wei4=[phone substringWithRange:NSMakeRange(phone.length-4,4)];
    return [NSString stringWithFormat:@"%@****%@",qian3,wei4];
}
#pragma mark - 去除小数点后多余的0
+ (NSString *)formatFloat:(float)f
{
    if (fmodf(f, 1)==0) {//如果有一位小数点
        return [NSString stringWithFormat:@"%.0f",f];
    } else if (fmodf(f*10, 1)==0) {//如果有两位小数点
        return [NSString stringWithFormat:@"%.1f",f];
    } else {
        return [NSString stringWithFormat:@"%.2f",f];
    }
}

#pragma mark - 32位 小写
+(NSString *)MD5ForLower32Bate:(NSString *)str{
    
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02x", result[i]];
    }
    
    return digest;
}

#pragma mark - 32位 大写
+(NSString *)MD5ForUpper32Bate:(NSString *)str{
    
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02X", result[i]];
    }
    
    return digest;
}
#pragma mark =================== json转jsonstring ===================
+ (NSString *)dictionaryToJson:(id)json_Data{
    if ([json_Data isEqual:[NSNull null]]) {
        return @"没有数据";
    }
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json_Data options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
#pragma mark =================== m模糊处理 ===================
+(void)solveUIWidgetFuzzy:(UIView *)view
{
    CGRect frame = view.frame;
    int x = floor(frame.origin.x);
    int y = floor(frame.origin.y);
    int w = floor(frame.size.width)+1;
    int h = floor(frame.size.height)+1;
    view.frame = CGRectMake(x, y, w, h);
}
@end
